package com.right.health.app.ui.advisory;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.DoctorBean;
import com.right.health.app.bean.FindDoctorResponse;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.net.DoctorApi;
import com.right.health.app.ui.advisory.adapter.MyDoctorAdapter;
import com.right.health.app.ui.registered.DoctorDetailActivity;
import com.right.health.app.ui.registered.FindDoctorActivity;

import java.util.ArrayList;
import java.util.List;

/*
 * Created by hopeliao on 2019-10-01
 *
 */
public class MyDoctorActivity extends BaseActivity implements OnHttpListener, SwipeRefreshLayout.OnRefreshListener {

    private MyDoctorAdapter mAdapter;

    private DoctorApi mDoctorApi;
    private SwipeListRefreshLayout mSwipeListRefreshLayout;
    private List<DoctorBean> mDoctorList;
    private EditText mSearchEt;
    private String mKeyword;
    private ListView listView;
    private View mEmptyView;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.my_doctor);
    }

    @Override
    public void findView() {
        mAdapter = new MyDoctorAdapter(this);
        listView = (ListView) findViewById(R.id.listview);
        mSwipeListRefreshLayout = findViewById(R.id.swiperefresh);
        mEmptyView = findViewById(R.id.empty_view);
        mSwipeListRefreshLayout.setOnRefreshListener(this);
        findViewById(R.id.tv_find).setOnClickListener(v -> FindDoctorActivity.launch(MyDoctorActivity.this,true));
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener((parent, view, position, id) -> DoctorDetailActivity.launch(MyDoctorActivity.this,mAdapter.getItem(position).getDoctorid(),true));
           listView.setEmptyView(mEmptyView);
/*        View headerView = LayoutInflater.from(this).inflate(R.layout.my_doctor_header, null);
        listView.addHeaderView(headerView);*/
        mSearchEt = findViewById(R.id.et_search);
        mSearchEt.setOnEditorActionListener((v, actionId, event)->{
            mKeyword = mSearchEt.getText().toString();
            mDoctorApi.focusDoctorList(mKeyword);
            mSwipeListRefreshLayout.setRefreshing(true);
            mSearchEt.clearFocus();
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(mSearchEt.getWindowToken(), 0);
            return true;
        });
    }

    @Override
    public void initialize() {
        mDoctorApi = new DoctorApi();
        mDoctorApi.setListener(this);
        mSwipeListRefreshLayout.setRefreshing(true);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        mSearchEt.setText(null);
        mKeyword = "";
        mDoctorApi.focusDoctorList(mKeyword);
    }

    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if (intent == BroadCastCode.REFRESH_MY_DOCTOR_LIST) {
            mSwipeListRefreshLayout.setRefreshing(true);
            onRefresh();
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (!response.isResponseFail()) {
            FindDoctorResponse doctorResponse = response.cast(FindDoctorResponse.class);
            if (mDoctorList == null) {
                mDoctorList = new ArrayList<>();
            }else {
                mDoctorList.clear();
            }
            mSwipeListRefreshLayout.onRefreshComplete();

            if (!CollectionUtil.isEmpty(doctorResponse.doctors)) {
                mDoctorList.addAll(doctorResponse.doctors);
            }

            mAdapter.setDataSource(mDoctorList);
        }else {
            ToastUtil.show(this, response.error);
        }

    }
}
